define(function (require,exports,module) {
    var myPub = require('./public');
    var oIdentificationTit = $('.js_identificationTit');        // 提示区域
    var oIdentifyName = $('.js_identifyName');                  // 姓名输入框
    var oIdentifyNum = $('.js_identifyNum');                    // 身份证号码输入框
    var oIdentifyBtn = $('.js_identifyBtn');                    // 去认证按钮
    var oBtnArea = $('.js_btnArea');                            // 按钮区域
    var oClearButn = $('.js_clearButn');                        // 清除按钮
    var oInput = $('.js_input');                                // 输入框
    var oEyeSee = $('.js_eyeSee');                              // 显示与隐藏的小眼睛
    var maxLength = 18;                                         // 身份证长度
    var bName = false;                                          // 验证姓名输入是否合规
    var bNUm = false;                                           // 验证身份证输入是否合规
    var bsubmitTap = true;                                      // 去认证按钮防止多次点击
    var nNumCon = '';                                           // 存储身份证号码
    var identifyUrl = '/m/u/user/idCard/1.0/personalIdentification';               // 验证接口

    var hrefUrl = window.location.href;
    var backHref = '';
    if(hrefUrl.indexOf('?backHref=') != -1){
        backHref = hrefUrl.split('?backHref=');
    }
    //加密
    var encrypt = function Encrypt(word) {
        var strInfo = "0102030405060708";
        var key = CryptoJS.enc.Utf8.parse(strInfo);
        var iv = CryptoJS.enc.Utf8.parse(strInfo);
        var srcs = CryptoJS.enc.Utf8.parse(word);
        var encrypted = CryptoJS.AES.encrypt(srcs, key, {iv: iv, mode: CryptoJS.mode.CBC});
        return encrypted.toString();
    };

    if((oIdentifyNum.val().length == 18) && (oIdentifyName.val() != '')){
        nNumCon = oIdentifyNum.val();
        oIdentificationTit.hide();
        oBtnArea.hide();
        encryption(oIdentifyNum.val(),oIdentifyNum);
        oInput.attr('disabled',true);
        oEyeSee.show();
        oClearButn.hide();
    }

    // 姓名验证
    oIdentifyName.on('input propertychange', function () {
        // 通过验证输入框内容，修改提示内容
        checkUserName(oIdentifyName,function() {
            bName = false;
        },function() {
            bName = false;
        },function() {
            if(oIdentifyName.val().length < 2){
                bName = false;
            }else{
                bName = true;
            }
        });

        // 满足条件 去认证按钮可点击
        if (bName && bNUm) {
            oIdentifyBtn.addClass('btnRed')
        } else {
            oIdentifyBtn.removeClass('btnRed')
        }

        // 清空按钮显示
        oIdentifyName.val().length != 0 ? oClearButn.eq(0).show() : oClearButn.eq(0).hide();
    });


    // 身份证验证
    oIdentifyNum.on('input propertychange', function () {
        // 通过验证输入框内容，修改提示内容
        iDCardNO(oIdentifyNum, function () {
            bNUm = false;
        }, function () {
            bNUm = false;
        },function () {
            if(oIdentifyNum.val().length == maxLength){
                bNUm = true;
            }else{
                bNUm = false;
            }
        });

        // 满足条件 去认证按钮可点击
        if (bName && bNUm) {
            oIdentifyBtn.addClass('btnRed')
        } else {
            oIdentifyBtn.removeClass('btnRed')
        }

        // 清空按钮显示
        oIdentifyNum.val().length != 0 ? oClearButn.eq(1).show() : oClearButn.eq(1).hide();
    });

    // 清除按钮
    oClearButn.on('click',function () {
        var index = oClearButn.index(this);
        oInput.eq(index).val('');
        oIdentifyBtn.removeClass('btnRed');
        if(index == 0){
            bName = false
        }else{
            bNUm = false
        }
        $(this).hide();
    });

    // 点击去认证按钮
    oIdentifyBtn.on('click',function (object) {
        if(!bsubmitTap)return;
        bsubmitTap = false;
        if(oIdentifyName.val() == ''){
            myPub.TipLayer('姓名不能为空！');
            bsubmitTap = true;
        }else if(!bName){
            myPub.TipLayer('请输入真实的姓名！');
            bsubmitTap = true;
        }else if(oIdentifyNum.val() == ''){
            myPub.TipLayer('身份证号码不能为空！');
            bsubmitTap = true;
        }else if(!bNUm){
            myPub.TipLayer('请输入18位身份证号码！');
            bsubmitTap = true;
        }else if($(this).hasClass('btnRed')){
            G_JSON_POST(identifyUrl,{
                'name': oIdentifyName.val(),
                'idCardNo': encrypt(oIdentifyNum.val())
            },function (data) {
                if(data.ret.code=='0'){
                    myPub.TipLayer('验证成功！');

                    nNumCon = oIdentifyNum.val();
                    oIdentificationTit.hide();
                    oBtnArea.hide();
                    encryption(oIdentifyNum.val(),oIdentifyNum);
                    oInput.attr('disabled',true);
                    oEyeSee.show();
                    oClearButn.hide();
                    // 如果是从确认订单页跳转过来的 跳转回确认订单页
                    if( backHref != '' && backHref[1] && backHref[1].length > 0){
                        window.location.href =  decodeURIComponent(backHref[1]);
                    }
                    bsubmitTap = true;
                }else{
                    myPub.TipLayer(data.ret.message);
                    bsubmitTap = true;
                }
            });

        }
    });

    // 点击小眼睛
    oEyeSee.on('click',function (object) {
        if($(this).hasClass('icon-eye_close')){
            $(this).addClass('icon-eye_look');
            $(this).removeClass('icon-eye_close');
            oIdentifyNum.val(nNumCon)
        }else{
            $(this).addClass('icon-eye_close');
            $(this).removeClass('icon-eye_look');
            encryption(nNumCon,oIdentifyNum)
        }
    });

    // 身份证号码加密
    function encryption(val,el) {
        var nfirst = val.slice(0, 4);
        var nend = val.slice(-4);
        el.val(nfirst + '**********' + nend);
    }

    // 身份证号码验证即数字验证
    function iDCardNO(obj, fn1, fn2, fn3) {
        var reg = /(^\d{17}(\d|X|x)$)|(^\d{0,18}$)/g;               // 0到18位数字或者17位数字加x
        if (!reg.test(obj.val())) {
            if (fn1) fn1();
        }else if(obj.val() == ''){
            if (fn2) fn2();
        } else {
            if (fn3) fn3();
        }
    }

    // 姓名验证即中文验证
    function checkUserName(obj, fn1, fn2, fn3){
        var _userName = obj.val();
        var reg = /^[\u4e00-\u9fa5]{1,4}$/;   //验证用户名

        if (_userName == '') {
            if (fn1) fn1()
        } else if (!reg.test(_userName)) {

            if (fn2) fn2();
        } else {
            if (fn3) fn3();
        }
    }
});